package com.example.service.impl;

import com.example.mapper.WxbOrderMapper;
import com.example.model.WxbOrder;
import com.example.service.WxbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class WxbOrderServiceImpl implements WxbOrderService {
    @Autowired
    private WxbOrderMapper wxbOrderMapper;
    @Override
    public boolean insertWxbOrder(WxbOrder wxbOrder) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        wxbOrder.setOrderId(format.format(date));
        wxbOrder.setOrderTime(date);
        wxbOrder.setAlipayState("0");
        return wxbOrderMapper.insert(wxbOrder)>0;
    }

    @Override
    public List<WxbOrder> findListByUserId(String userId) {
        Example example = new Example(WxbOrder.class);
        example.and().andEqualTo("userId",userId);
        List<WxbOrder> wxbOrders = wxbOrderMapper.selectByExample(example);

        return wxbOrders;
    }

    @Override
    public WxbOrder findByOrderId(String orderId) {
        Example example = new Example(WxbOrder.class);
        example.and().andEqualTo("orderId",orderId);
        WxbOrder wxbOrder = wxbOrderMapper.selectOneByExample(example);
        return wxbOrder;
    }

    @Override
    public boolean updateOrder(WxbOrder wxbOrder) {
        return wxbOrderMapper.updateByPrimaryKeySelective(wxbOrder)==1;
    }
}
